package com.seanzx.dp.processor;

import com.seanzx.dp.common.Persist;
import com.seanzx.common.Result;
import com.seanzx.common.ResultCode;

import java.io.File;
import java.io.IOException;

public class TableProcessor {

    public Result<Integer> create(String databaseName, String tableName) throws IOException {
        File file = Persist.getTable(databaseName, tableName);
        if (file.exists()) {
            return Result.of(ResultCode.TABLE_ALREADY_EXISTS, 0);
        }
        if (!file.createNewFile()) {
            return Result.of(ResultCode.CREATE_TABLE_FAILED, 0);
        }
        return Result.ok(1);
    }

    public Result<Integer> drop(String databaseName, String tableName) throws IOException {
        File file = Persist.getTable(databaseName, tableName);
        if (!file.exists()) {
            return Result.of(ResultCode.TABLE_NOT_EXISTS, 0);
        }
        if (!file.delete()) {
            return Result.of(ResultCode.DELETE_DATA_FAILED, 0);
        }
        return Result.ok(1);
    }
}
